/*
 * To change this template, choose Tools | Templates
 *  and open the template in the editor.
 */
package projetomsn.Client.Settings;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 * Classe de configurações
 *    @author Administrator
 */
public class SettingsFile {

    /**
     * Atributo de nome do arquivo
     */
    private String fileName;
    /**
     * Atributo de caminho do arquivo
     */
    private String path;
    /**
     * 
     */
    private FileOutputStream output;
    /**
     * 
     */
    private BufferedReader reader;
    /**
     * Declaração de uma lista
     */
    private ArrayList<String> list;
    /**
     * Driver do banco de dados
     */
    static final String DB_DRIVER = "com.mysql.jdbc.Driver";
    /**
     * URL do banco de dados
     */
    static final String DB_URL = "jdbc:mysql://localhost:3306/humberto";
    /**
     * Usuário do bando de dados
     */
    static final String DB_USER = "root";
    /**
     * Senha do banco de dados
     */
    private static String DB_PASSWORD = "0379981";
    /**
     * Atributo de conexão
     */
    private static Connection connection;
    /**
     * 
     */
    private Statement statement;
    /**
     * 
     */
    private ResultSet resultSet;

    /**
     * Metodo de configuração
     * @param fileName
     * @param path 
     */
    public SettingsFile(String fileName, String path) {
        this.fileName = fileName;
        this.path = path;
        list = new ArrayList<String>();

    }

    /**
     * Metodo de conexão com o banco de dados
     */
    public static void connect() {

        try {
            Class.forName(DB_DRIVER);
            connection = (Connection) DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
            //JOptionPane.showMessageDialog(null, "Succesfully connect to MySQL");

        } catch (ClassNotFoundException exceptionDriver) {

            //JOptionPane.showMessageDialog(null, "Driver not found!");
        } catch (SQLException ex) {

            //JOptionPane.showMessageDialog(null, "DB not found!");

        }


    }

    /**
     * Metodo para desconexão com o banco de dados
     */
    public static void desconnect() {

        try {
            connection.close();

        } catch (SQLException erroSQL) {
        }
    }

    /**
     * Metodo para a excução do banco de dados
     * @param sql 
     */
    public void executeSQL(String sql) {
        try {
            statement = (Statement) connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            resultSet = statement.executeQuery(sql);
        } catch (SQLException sqlex) {
            sqlex.printStackTrace();
        }
    }

    /**
     * Meotodo para inserção de configuração de tamanho, estilo, cor, negrito,
     * itálico e sublinado ao usuário.
     * @param id
     * @param color
     * @param style
     * @param size
     * @param b
     * @param i
     * @param u
     * @throws SQLException 
     */
    public static void insert(String id, String color,
            String style, int size, boolean b, boolean i, boolean u) throws SQLException {
        String query = "INSERT INTO humberto.font_settings VALUE ("
                + "'" + id + "'"
                + ",'" + color + "'"
                + ",'" + style + "'"
                + ",'" + size + "'"
                + ",'" + b + "'"
                + ",'" + i + "'"
                + ",'" + u + "') "
                + "";

        Statement st = (Statement) connection.createStatement();

        // execute the query, and get a java resultSet
        int rs = st.executeUpdate(query);
    }

    /**
     * Metodo de atualização das novas configurações do usuário
     * @param id
     * @param color
     * @param style
     * @param size
     * @param b
     * @param i
     * @param u
     * @throws SQLException 
     */
    public static void update(String id, String color,
            String style, int size, boolean b, boolean i, boolean u) throws SQLException {
        String query = "UPDATE font_settings SET "
                + "color='" + color + "'"
                + ",style='" + style + "'"
                + ",size=" + size + ""
                + ",bold='" + b + "'"
                + ",italic='" + i + "'"
                + ",underline='" + u + "' "
                + "WHERE id='" + id + "'";

        Statement st = (Statement) connection.createStatement();

        // execute the query, and get a java resultSet
        int rs = st.executeUpdate(query);
    }

    /**
     * Metodo que retorna as configurações do usuário.
     * @param id
     * @param setting
     * @return 
     */
    public static String getSetting(String id, String setting) {

        String query = "SELECT " + setting + " FROM humberto.font_settings WHERE id='" + id + "'";
        String s = "";
        // create the java statement


        try {
            Statement st = (Statement) connection.createStatement();
            ResultSet rs = st.executeQuery(query);
            if (rs.next());
            s = rs.getString(setting);
            st.close();
            // iterate through the java resultSet
        } catch (SQLException ex) {
            Logger.getLogger(SettingsFile.class.getName()).log(Level.SEVERE, null, ex);
        }
        return s;
    }

}
